package com.hmc.kafka.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * 自定义分区规则
 * The customize partitioning strategy
 * <ul>
 *     <li>当配置了分区号时，使用分区</li>
 *     <li>当未配置分区号时，值中有hmc的放入0号分区，其他的放入1号分区</li>
 * </ul>
 */
public class MyPartitioner implements Partitioner {
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        int partition;
        if (value==null)
            partition = 1;
        String s = value.toString();
        if (s.contains("hmc"))
            partition = 0;
        else
            partition = 1;
        return partition;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
